﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project_2._2.Models;
using Project_2._2.ViewModels;
using System.IO;
using Project_2._2.Extra;
using System.Web.Security;
using PagedList;
using PagedList.Mvc;
namespace Project_2._2.Controllers
{
    public class AdminBookCarController : Controller
    {
        private QuanLyThueXeEntities db = new QuanLyThueXeEntities();
        private Common cm = new Common();
        #region Default
        // GET: /Adminbookcars/
        public ActionResult Default(int? page, string rd, string Record, string key, string SBy, string SType)
        {
           
            try
            {
                if (!Request.IsAuthenticated)
                {
                    return RedirectToAction("Login", "Admin");
                }
                else
                {
                    if (cm.CheckUserType(User.Identity.Name))
                    {
                        if (rd != null)
                        {
                            int soDong = int.Parse(rd);
                            string k = key;
                            string b = SBy;
                            string t = SType;
                            IPagedList<DatXe> bookcars = null;
                            if (!string.IsNullOrEmpty(k) && b != "0")
                            {
                                ViewBag.Key = k;
                                ViewBag.SBy = b;
                                ViewBag.SType = t;

                                switch (b)
                                {
                                    case "Name":
                                        if (t == "1")
                                            bookcars = (from p in db.DatXes where p.HoTen.Contains(key) && p.isDelete == false select p).OrderByDescending(m => m.DatXeId).ToList().ToPagedList(page ?? 1, soDong);
                                        else
                                        {
                                            bookcars = (from p in db.DatXes where p.HoTen.Equals(key) && p.isDelete == false select p).OrderByDescending(m => m.DatXeId).ToList().ToPagedList(page ?? 1, soDong);
                                        }
                                        break;
                                    case "Phone":
                                        if (t == "1")
                                            bookcars = (from p in db.DatXes where p.DienThoai.Contains(key) && p.isDelete == false select p).OrderByDescending(m => m.DatXeId).ToList().ToPagedList(page ?? 1, soDong);
                                        else
                                        {
                                            bookcars = (from p in db.DatXes where p.DienThoai.Equals(key) && p.isDelete == false select p).OrderByDescending(m => m.DatXeId).ToList().ToPagedList(page ?? 1, soDong);
                                        }
                                        break;
                                    case "Email":
                                        if (t == "1")
                                            bookcars = (from p in db.DatXes where p.Email.Contains(key) && p.isDelete == false select p).OrderByDescending(m => m.DatXeId).ToList().ToPagedList(page ?? 1, soDong);
                                        else
                                        {
                                            bookcars = (from p in db.DatXes where p.Email.Equals(key) && p.isDelete == false select p).OrderByDescending(m => m.DatXeId).ToList().ToPagedList(page ?? 1, soDong);
                                        }
                                        break;
                                }
                            }
                            else
                            {
                                bookcars = (from p in db.DatXes where p.isDelete == false select p).OrderByDescending(m => m.DatXeId).ToList().ToPagedList(page ?? 1, soDong);
                            }

                            ViewBag.record = rd;
                            return View(bookcars);
                        }
                        else
                        {
                            var bookcars = (from p in db.DatXes where p.isDelete == false select p).OrderByDescending(m => m.DatXeId).ToList().ToPagedList(page ?? 1, 10);
                            return View(bookcars);
                        }
                    }
                    else
                        return RedirectToAction("Login", "Admin");
                }
            }
            catch (Exception)
            {
                return RedirectToAction("SystemErrors", "Default");
            }
        }
        //POST
        [HttpPost]
        public ActionResult Default(FormCollection col, int? page)
        {
            try
            {
                int soDong = int.Parse(col["soDong"]);
                string SearchBy = col["SearchBy"];
                string SearchType = col["SearchType"];
                string key = col["KeyWorld"];
                IPagedList<DatXe> bookcars = null;

                //
                ViewBag.Key = key;
                ViewBag.SBy = SearchBy;
                ViewBag.SType = SearchType;
                ViewBag.record = soDong.ToString();

                if (string.IsNullOrEmpty(key))
                {
                    bookcars = (from p in db.DatXes where p.isDelete == false select p).OrderByDescending(m => m.DatXeId).ToList().ToPagedList(page ?? 1, soDong);
                    return View(bookcars);
                }
                else
                {

                    switch (SearchBy)
                    {

                        case "Name":
                            if (SearchType == "1")
                                bookcars = (from p in db.DatXes where p.HoTen.Contains(key) && p.isDelete == false select p).OrderByDescending(m => m.DatXeId).ToList().ToPagedList(page ?? 1, soDong);
                            else
                            {
                                bookcars = (from p in db.DatXes where p.HoTen.Equals(key) && p.isDelete == false select p).OrderByDescending(m => m.DatXeId).ToList().ToPagedList(page ?? 1, soDong);
                            }
                            break;
                        case "Phone":
                            if (SearchType == "1")
                                bookcars = (from p in db.DatXes where p.DienThoai.Contains(key) && p.isDelete == false select p).OrderByDescending(m => m.DatXeId).ToList().ToPagedList(page ?? 1, soDong);
                            else
                            {
                                bookcars = (from p in db.DatXes where p.DienThoai.Equals(key) && p.isDelete == false select p).OrderByDescending(m => m.DatXeId).ToList().ToPagedList(page ?? 1, soDong);
                            }
                            break;
                        case "Email":
                            if (SearchType == "1")
                                bookcars = (from p in db.DatXes where p.Email.Contains(key) && p.isDelete == false select p).OrderByDescending(m => m.DatXeId).ToList().ToPagedList(page ?? 1, soDong);
                            else
                            {
                                bookcars = (from p in db.DatXes where p.Email.Equals(key) && p.isDelete == false select p).OrderByDescending(m => m.DatXeId).ToList().ToPagedList(page ?? 1, soDong);
                            }
                            break;
                        default:
                            ViewBag.erorr = "Không có dữ liệu .... ";
                            break;
                    }
                    return View(bookcars);
                }
            }
            catch (Exception)
            {
                return RedirectToAction("SystemErrors", "Default");
            }

        }
        #endregion
        [HttpPost]
        public ActionResult Multi_Delete(IEnumerable<int> DatXeIdsToDelete)
        {
            if (!Request.IsAuthenticated)
            {
                return RedirectToAction("Login", "Admin");
            }
            else
            {
                if (cm.CheckUserType(User.Identity.Name))
                {
                    try
                    {
                        var Bcar_delete = db.DatXes.Where(x => DatXeIdsToDelete.Contains(x.DatXeId)).ToList();
                        foreach (var Bcar in Bcar_delete)
                        {
                            Bcar.isDelete = true;
                            UpdateModel(Bcar);
                            db.SaveChanges();
                        }
                        return RedirectToAction("Default", "AdminBookCar");
                    }
                    catch (Exception)
                    {
                        return RedirectToAction("SystemErrors", "Default");
                    }
                }
                else
                    return RedirectToAction("Login", "Admin");
            }         

        }

        public ActionResult Accept(int id)
        {
            try
            {
                DatXe Bcar = (from p in db.DatXes where p.DatXeId == id select p).First();
                Bcar.Duyet = true;
                UpdateModel(Bcar);
                db.SaveChanges();
                return RedirectToAction("Default", "AdminBookCar");
            }
            catch (Exception)
            {
                return RedirectToAction("SystemErrors", "Default");
            }
        }

        public ActionResult Delete(int id)
        {
            if (!Request.IsAuthenticated)
            {
                return RedirectToAction("Login", "Admin");
            }
            else
            {
                if (cm.CheckUserType(User.Identity.Name))
                {
                    try
                    {

                        DatXe Bcar = (from p in db.DatXes where p.DatXeId == id select p).First();
                        Bcar.isDelete = true;
                        UpdateModel(Bcar);
                        db.SaveChanges();
                        return RedirectToAction("Default", "AdminBookcar");
                    }
                    catch (Exception)
                    {
                        return RedirectToAction("SystemErrors", "Default");
                    }
                }
                else
                    return RedirectToAction("Login", "Admin");
            }
            
        }

        public ActionResult Details(int id)
        {
            try
            {
                var datxe = (from p in db.DatXes where p.DatXeId == id select p).FirstOrDefault();
                return View(datxe);
            }
            catch (Exception)
            {
                return RedirectToAction("SystemErrors", "Default");
            }
        }
    }
}
